HLSL TangentToWorld.hlsl
https://gyazo.com/bab134a3c40d98fafa62fd8ff8af5a73
code:HLSL TangentToWorld.hlsl
float4x4 matW; // The World matrix
// Need a sampler to fetch from the normal map
sampler sNormalMap;
struct PSInupt
{
float4 color : COLOR0;
float2 uv : TEXCOORD0;
// Import world position, tangent and normal
float3 worldPos : COLOR1;
float3 tangent : TEXCOORD1;
float3 normal : TEXCOORD2;
};
float4 PixelShader(inPSInput input) : COLOR0
{
// Setup the temporary color register
float4 tmpColor = input.color;
// Fetch the normal from the texture
// Need to bias the result to get it
// in the range of -1 to 1
float3 tmpNormal = 2.0 * (tex2D(sNormalMap, input.uv) - 0.5);
// Renormalize our resulting inputs and get binormal
float3 tangent = normalize(input.tangent);
flaot3 normal = normalize(input.normal);
flaot3 binormal = normalize(cross(tangent, normal));
//Compute matrinx inverse
Float3x3 matInverse = transpose(float3x3(tangent, binormal, normal));
// Bring normal into world space
normal = mul(mul(tmpNormal, matInverse), matW);
}